﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.Reflection;


namespace EraNet.Common
{
    /// <summary>
    ///JSONHelper 的摘要说明
    /// </summary>
    public static class JSONHelper
    {
        /// <summary>
        /// 将对象序列化成JSON格式字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJSON(this object obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }


        /// <summary>
        /// 将JSON格式字符串反序列化成相应的T类型对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T ParseJSON<T>(this string str)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                return (T)serializer.ReadObject(ms);
            }
        }

        /// <summary>
        /// 将对象序列化成JSON格式字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJSONjss(this object obj)
        {

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(obj);
        }

        /// <summary>
        /// 将对象序列化成JSON格式字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="recursionDepth"></param>
        /// <returns></returns>
        public static string ToJSONjss(this object obj, int recursionDepth)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            serializer.RecursionLimit = recursionDepth;
            return serializer.Serialize(obj);
        }

        /// <summary>
        /// 将JSON格式字符串反序列化成相应的T类型对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T ParseJSONjss<T>(this string str)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<T>(str);
        }


        /// <summary>
        /// 将JSON格式字符串反序列化成相应的T类型的List泛型集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<T> ParseJSONList<T>(this string str)
        {
            List<T> lst = new List<T>();
            if (str.Substring(0, 1) == "[")
            {
                string s1 = str.Remove(str.Length - 1, 1).Remove(0, 1);
                str = s1.Replace("},{", "};{");
            }
            string[] strs = str.Split(';');
            for (int i = 0; i < strs.Length; i++)
            {
                T t = strs[i].ParseJSONjss<T>();
                lst.Add(t);
            }
            return lst;
        }

        /// <summary>
        /// 将JSON 反序化成 List<T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<T> DeserializeList<T>(this string str)
        {
            List<T> lst = new List<T>();
            if (str.Substring(0, 1) == "[")
            {
                string s1 = str.Remove(str.Length - 1, 1).Remove(0, 1);
                str = s1.Replace("},{", "};{");
            }
            string[] strs = str.Split(';');
            for (int i = 0; i < strs.Length; i++)
            {
                T t = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(strs[i]);
                lst.Add(t);
            }
            return lst;
        }

        public static string ToJson(this object obj, int recursionDepth = 100)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            serializer.RecursionLimit = recursionDepth;
            return serializer.Serialize(obj);
        }

        public static List<T> ToListObject<T>(this string obj, int recursionDepth = 100)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            serializer.RecursionLimit = recursionDepth;
            List<T> returnList = serializer.Deserialize<List<T>>(obj);
            return returnList;
        }
    }

}

